import { GET_MENU, SET_LV2_MENU, SET_LV3_MENU, GET_USERINFO, GET_GROUP_MENU, SET_LV3_ID, SET_BREAD_TAIL } from './mutation-types'
// 转换menu数据格式
const mapMenu = function (menu) {
  let obj = {}
  for (let i = 0; i < menu.length; i++) {
    obj[menu[i].id] = menu[i]
    if (menu[i].child) {
      menu[i].child = mapMenu(menu[i].child)
    }
  }
  return obj
}
// 获取所有父节点
// const getParents = function (arr, target) {
//   var result = []
//   for (var i = 0, length = arr.length; i < length; i++) {
//     result.push(arr[i].id)
//     if (arr[i].id === target) {
//       return result
//     } else if (arr[i].child) {
//       var arr2 = getParents(arr[i].child, target)
//       if (arr2.length) {
//         result = result.concat(arr2)
//         return result
//       } else {
//         result = []
//       }
//     } else {
//       result = []
//     }
//   }
//   return result
// }

export default {
  [GET_MENU] (state, payload) {
    state.menu = mapMenu(payload.menu)
  },
  [SET_LV2_MENU] (state, id) {
    state.lv2MenuId = id
    localStorage.setItem('lv2Menu', id)
  },
  [SET_LV3_MENU] (state, id) {
    state.lv3MenuId = id
    localStorage.setItem('lv3Menu', id)
    state.lv3Id = null
    localStorage.removeItem('lv3Id')
  },
  [SET_LV3_ID] (state, id) {
    state.lv3Id = id
    localStorage.setItem('lv3Id', id)
  },
  [GET_USERINFO] (state, payload) {
    state.userInfo = payload
  },
  [GET_GROUP_MENU] (state, payload) {
    state.groupMenu = payload
  },
  [SET_BREAD_TAIL] (state, payload) {
    state.breadTail = payload
  }
}
